package cn.quevo.core.exception;

import cn.quevo.core.entity.ResultEntity;
import cn.quevo.core.enums.EnumsInterface;
import cn.quevo.core.lang.NumberUtils;
import cn.quevo.core.lang.StringUtils;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 自定义异常，必须明确异常抛出
 * 
 * @author July july_sky@foxmail.com
 * @date 2017年10月12日 下午5:43:50
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @version 1.0
 */
@Data
@Builder
@EqualsAndHashCode(callSuper = true)
public class CustomException extends RuntimeException {

    /**
     * 
     */
    private static final long serialVersionUID = -46082100909100689L;

    /** 状态码 */
    private Integer status;
    /** 提示信息 */
    private String msg;

    public CustomException(String msg) {
        this(5000, msg);
    }

    public CustomException(String msg, Throwable cause) {
        this(5000, msg, cause);
    }

    public CustomException(Integer status, String msg) {
        this(status, msg, null);
    }

    public CustomException(Integer status, String msg, Throwable cause) {
        super(msg, cause);
        this.status = status;
        this.msg = msg;
    }

    public CustomException(ResultEntity result) {
        super(result.getMsg());
        this.status = result.getStatus();
        this.msg = result.getMsg();
    }

    public CustomException(EnumsInterface enums) {
        this(NumberUtils.toInt(StringUtils.valueOf(enums.getCode()), 50000), enums.getValue());
    }
    
    public CustomException(EnumsInterface enums,Throwable cause) {
        this(NumberUtils.toInt(StringUtils.valueOf(enums.getCode()), 50000), enums.getValue(),cause);
    }

    @Override
    public String toString() {
        return StringUtils.isBlank(status) ? this.msg : "[".concat(this.status + "").concat("]").concat(this.msg);
    }

    @Override
    public String getMessage() {
        return this.toString();
    }
}
